﻿using Microsoft.Extensions.DependencyInjection;
using PersonalWebsite.Core.CQRS;
using PersonalWebsite.Core.DependencyInjection;
using PersonalWebsite.Core.Vaildate;
using Volo.Abp.Modularity;

namespace PersonalWebsite.Core;

public class CoreModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddIOC();
        AddCommandAndQuery(context.Services);
    }

    /// <summary>
    /// 添加命令与查询
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    private void AddCommandAndQuery(IServiceCollection services)
    {
        var option = IOC.GetOption<CQRSOption>();

        services.AddMediatR(c =>
        {
            c.AddOpenBehavior(typeof(VaildateBehavior<,>));
            if (option.BehaviorTypes != null && option.BehaviorTypes.Length != 0)
            {
                foreach (var behaviorType in option.BehaviorTypes)
                    c.AddOpenBehavior(behaviorType);
            }

            c.RegisterServicesFromAssemblies(option.Assemblies);
        });
    }
}
